﻿/**************************************************************************
创建时间:	2020/5/14
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

Copyright (c) zhcun.cn

描	述：
记	录：
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using ZhCun.DbCore;
using ZhCun.DbCore.BuildSQLText;
using ZhCun.DbCore.Cores;
using ZhCun.DBModel;
using ZhCun.Utils;
using ZhCun.Utils.Helpers;
using ZhCun.Win.DB;

namespace ZhCun.Win.ExtendGridView
{
    class GridViewBLL : BLLBase
    {
        public string SearchForm { get; internal set; }

        /// <summary>
        /// 根据指定form的所有列信息，可能包含多个GridView信息
        /// </summary>
        /// <param name="formName">窗体名称</param>
        /// <returns></returns>
        public List<Sys_GridColumn> GetColumnInfo(string formName)
        {
            var query = DB.CreateQuery<Sys_GridColumn>();
            query.WhereAnd(s => s.FormName == formName);
            query.OrderBy(s => s.ColumnIndex);
            var colList = DB.Query<Sys_GridColumn>(query).ToList(true);
            return colList;
        }

        /// <summary>
        /// 根据form名称及dgv名称获取列名
        /// </summary>
        /// <param name="formName">窗体名称</param>
        /// <param name="dgvName">dgv名称</param>
        /// <returns></returns>
        public List<Sys_GridColumn> GetColumnInfo(string formName, string dgvName)
        {
            var query = DB.CreateQuery<Sys_GridColumn>();
            query.WhereAnd(s => s.FormName == formName && s.DgvName == dgvName);
            query.OrderBy(s => s.ColumnIndex);
            var colList = DB.Query<Sys_GridColumn>(query).ToList(true);
            return colList;
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        public DataTable GetData(int pageNo, int pageSize, string searchVal, out int rowCount)
        {
            var query = DB.CreateQuery<Sys_GridColumn>();
            query.PageNo = pageNo;
            query.PageSize = pageSize;

            if (!SearchForm.IsEmpty())
            {
                query.WhereAnd(s => s.FormName == SearchForm);
            }

            if (!searchVal.IsEmpty())
            {
                string likeStr = $"%{searchVal}%";
                query.WhereAnd(s =>
                    s.WEx_Like(s.FormName, likeStr) ||
                    s.WEx_Like(s.DgvName, likeStr) ||
                    s.WEx_Like(s.ColumnName, likeStr) ||
                    s.WEx_Like(s.ColumnTitle, likeStr)
                    );
            }
            query.OrderBy(s => s.FormName)
                 .OrderBy(s => s.DgvName)
                 .OrderBy(s => s.ColumnIndex);

            var ret = DB.Query<Sys_GridColumn>(query, ADJoinSql);
            var dt = ret.ToDataTable();
            rowCount = query.Total;
            return dt;
        }
        /// <summary>
        /// 新增\修改 的model验证
        /// </summary>
        protected ApiResult<string> VerifyModel(Sys_GridColumn model, bool isAdd)
        {
            if (model.FormName.IsEmpty())
            {
                return ApiResult.RetErr(nameof(model.FormName), "窗体名称不能为空");
            }
            if (model.DgvName.IsEmpty())
            {
                return ApiResult.RetErr(nameof(model.DgvName), "Grid名称不能为空");
            }
            if (model.ColumnName.IsEmpty())
            {
                return ApiResult.RetErr(nameof(model.ColumnName), "列名不能为空");
            }
            if (model.ColumnTitle.IsEmpty())
            {
                return ApiResult.RetErr(nameof(model.ColumnTitle), "列标题不能为空");
            }
            if (model.ColumnIndex < 0)
            {
                return ApiResult.RetErr(nameof(model.ColumnIndex), "列索引必须大于0");
            }
            if (model.ColumnType.IsEmpty())
            {
                return ApiResult.RetErr(nameof(model.ColumnType), "列类型不能为空");
            }
            if (model.IsAdSearch)
            {
                if (model.DataType.IsEmpty())
                {
                    return ApiResult.RetErr(nameof(model.DataType), "搜索列可用时,必选选择数据类型");
                }
            }
            //if (isAdd)
            //{
            //    model.ClearChangedState();
            //    model.Id = null;
            //}
            if (DB.QueryExist<Sys_GridColumn>(s =>
                    s.FormName == model.FormName &&
                    s.DgvName == model.DgvName &&
                    s.ColumnName == model.ColumnName &&
                    s.Id != model.Id))
            {
                return ApiResult.RetErr(nameof(model.ColumnIndex), "列名重复");
            }
            return ApiResult.RetOK("", "保存成功");
        }
        /// <summary>
        /// 新增
        /// </summary>
        public virtual ApiResult<string> Add(Sys_GridColumn model)
        {
            var ret = VerifyModel(model, true);
            if (ret)
            {
                DB.Insert(model);
            }
            return ret;
        }
        /// <summary>
        /// 更新
        /// </summary>
        public virtual ApiResult<string> Edit(Sys_GridColumn model)
        {
            var ret = VerifyModel(model, false);
            if (ret)
            {
                DB.Update(model);
            }
            return ret;
        }
        /// <summary>
        /// 删除
        /// </summary>
        public ApiResult Del(Sys_GridColumn model)
        {
            var r =
            DB.Delete<Sys_GridColumn>(s =>
                s.FormName == model.FormName &&
                s.DgvName == model.DgvName &&
                s.ColumnIndex == model.ColumnIndex);
            if (r.RowCount == 1)
            {
                return ApiResult.RetOK("删除成功");
            }
            else
            {
                return ApiResult.RetErr("删除失败");
            }
        }
    }
}
